package com.soilflow.info.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.soilflow.common.domain.LandContract;
import com.soilflow.common.domain.LandPaymentPlan;
import com.soilflow.common.domain.LandRent;
import com.soilflow.common.domain.dto.LandRentBeOverdueDto;
import com.soilflow.common.domain.vo.LandRentBeOverdueVo;
import com.soilflow.info.mapper.LandPaymentPlanMapper;
import com.soilflow.info.mapper.LandRentBeOverdueMapper;
import com.soilflow.info.service.ILandRentService;
import com.soilflow.info.service.LandContractService;
import com.soilflow.info.service.LandPaymentPlanService;
import com.soilflow.info.service.LandRentBeOverdueService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author chengshihua
 */
@Service
public class LandRentBeOverdueServiceImpl implements LandRentBeOverdueService {
    @Resource
    private LandRentBeOverdueMapper landRentBeOverdueMapper;
    @Resource
    private LandPaymentPlanService landPaymentPlanService;
    @Resource
    private ILandRentService landRentService;
    @Resource
    private LandContractService landContractService;


    @Override
    public List<LandRentBeOverdueVo> list(LandRentBeOverdueDto dto) {

        // 1. 查出在今天之前付款计划未付款的所有计划
        QueryWrapper<LandPaymentPlan> landPaymentPlanQueryWrapper = new QueryWrapper<>();
        landPaymentPlanQueryWrapper.lambda().le(LandPaymentPlan::getEndTime,new Date());
        List<LandPaymentPlan> paymentPlanList = landPaymentPlanService.list(landPaymentPlanQueryWrapper);

        // 2. 根据土地id查看已收租金是否未确认
        for (LandPaymentPlan landPaymentPlan : paymentPlanList) {
            QueryWrapper<LandRent> landRentQueryWrapper = new QueryWrapper<>();
            // 2.1 根据合同查看土地id
            QueryWrapper<LandContract> landContractQueryWrapper = new QueryWrapper<>();
            landContractQueryWrapper.lambda().eq(LandContract::getId,landPaymentPlan.getContractId());
            LandContract landContract = landContractService.getOne(landContractQueryWrapper);
            // 2.2 已收租金中查看是否已经付款
            landRentQueryWrapper.lambda().eq(LandRent::getLandId,landContract.getInfoId());


        }

        return landRentBeOverdueMapper.list(dto);
    }
}
